package com.skyworth.web.admin.api;

import com.skyworth.web.admin.role.domain.RoleModel;
import com.skyworth.web.admin.role.form.RoleForm;
import com.skyworth.web.admin.role.service.RoleService;
import com.skyworth.web.admin.user.domain.UserModel;
import com.skyworth.web.common.controller.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 移动端菜品类别管理
 * Created by skyworth on 2016/11/10.
 */
@Controller
@RequestMapping("madmin")
public class ApiRoleController extends BaseController{

	private static final Logger log = LoggerFactory.getLogger(ApiRoleController.class);

	@Autowired
	private RoleService roleService;

	/*
	菜品类别列表
	 */
	@RequestMapping("/api/role/list")
	@ResponseBody
	public Map<String, Object>  index(Model model) {
		Map<String, Object> result = new HashMap<>();
		UserModel userModel = currentUser();
		long store_id = 0;
		if (userModel != null) {
			store_id = userModel.storeId;
		}
		List<RoleModel> roleModelList = roleService.listRole(store_id);
		result.put("roles", roleModelList);
		return result;
	}

	/*
	角色添加
	 */
	@RequestMapping(value = {"/api/role/create"}, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> create(HttpServletResponse response, @ModelAttribute RoleForm roleForm, Model model) {
		Map<String, Object> result = new HashMap<>();
		if (roleForm == null) {
			result.put("errorMsg", "error");
			result.put("valid", false);
			return result;
		}
		UserModel userModel = currentUser();
		if (null == userModel) {
			result.put("valid", false);
			result.put("errorMsg", "Login first");
			return result;
		}

		long store_id = userModel.storeId;

		if (roleService.existRole(roleForm.getName(), store_id)) {
			result.put("errorMsg", roleForm.getName() + "已存在");
			result.put("valid", false);
			return result;
		}
		roleForm.setStoreId(store_id);
		RoleModel roleModel = roleService.addRole(roleForm);

		if (null != roleModel && roleModel.id > 0) {
			List<RoleModel> roleModelList = roleService.listRole(store_id);
			result.put("valid", true);
			result.put("roles", roleModelList);

		} else {
			result.put("valid", false);
			result.put("errorMsg", "添加类别失败");
		}
		return result;
	}


	/*
	修改角色
	 */
	@RequestMapping(value = "/api/role/{id}", method = RequestMethod.PUT)
	@ResponseBody
	public Map<String, Object> update(@PathVariable("id") long id,
									  @ModelAttribute("role") RoleForm roleForm,
									  Model model) {
		Map<String, Object> result = new HashMap<>();
		UserModel userModel = currentUser();
		if (userModel == null) {
			result.put("valid", false);
			result.put("errorMsg", "Login first");
			return result;
		}

		if(roleService.existRole(roleForm.getName(), userModel.storeId)){
			result.put("valid", false);
			result.put("errorMsg", roleForm.getName()+"已存在");
			return result;
		}
		roleForm.id = id;
		RoleModel roleModel = roleService.updateRole(roleForm);

		result.put("valid", true);
		result.put("role", roleModel);
		return result;
	}



}
